﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SpeedTestReports.XAMLReports;
using System.IO;
using System.IO.IsolatedStorage;
using System.Security.Cryptography;
using System.Security.Permissions;

namespace SpeedTestReports
{
    public partial class LoginForm : ChildWindow
    {
        private string userName = String.Empty;
        private string password = String.Empty;

        public LoginForm()
        {
            InitializeComponent();
        }

        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
            }
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            //TODO: Rijndael encryption
            userName = txtUserName.Text;
            password = txtPassword.Password;

            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite();

            if ((bool)chkRememberMe.IsChecked)
            {
                if (storage != null)
                {
                    if (!storage.DirectoryExists("SpeedTest"))
                    {
                        storage.CreateDirectory("SpeedTest");
                    }
                    IsolatedStorageFileStream fs = new IsolatedStorageFileStream(System.IO.Path.Combine("SpeedTest", "UserData.txt"), FileMode.OpenOrCreate, storage);
                    StreamWriter writer = new StreamWriter(fs);

                    writer.WriteLine(userName);
                    writer.WriteLine(password);

                    writer.Flush();
                    writer.Close();
                }
            }
            else
            {
                if (storage != null)
                {
                    if (!storage.DirectoryExists("SpeedTest") && storage.FileExists(System.IO.Path.Combine("SpeedTest", "UserData.txt")))
                    {
                        storage.DeleteFile(System.IO.Path.Combine("SpeedTest", "UserData.txt"));
                    }
                }
            }

            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            RegistrationForm register = new RegistrationForm();
            register.Show();
        }

        private void btnForgotPassword_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("Please enter your username");
                return;
            }

            MessageBoxResult res = MessageBox.Show("The server will now send an email reminder to the address on record", 
                "Confirm", MessageBoxButton.OKCancel);
            
            if (res == MessageBoxResult.OK)
            {
                this.Cursor = Cursors.Wait;
                XAMLReportsSoapClient client = new XAMLReportsSoapClient();
                client.ResendPasswordCompleted += new EventHandler<ResendPasswordCompletedEventArgs>(client_ResendPasswordCompleted);
                client.ResendPasswordAsync(txtUserName.Text);
            }
        }

        void client_ResendPasswordCompleted(object sender, ResendPasswordCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //TODO: do something
            }

            this.Cursor = Cursors.Arrow;

            if (e.Result)
            {
                MessageBox.Show("Message Sent");
            }
            else
            {
                MessageBox.Show("The attempt to send the email failed. Plese contact the system administrator.");
            }
        }

        private void FormLogin_Loaded(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(password) && !String.IsNullOrEmpty(userName)) 
            { 
                txtPassword.Password = password; 
                txtUserName.Text = userName;

                //they are remembered from a previous save, so set the check box
                chkRememberMe.IsChecked = true;
            }
        }
    }
}

